def length_of_longest_substring(arr, k):
  start = 0 
  max_length = 0
  Frequency_map = {}
  
  for end in range(len(arr)):
    right_num = arr[end]
    if (right_num not in Frequency_map):
      Frequency_map[right_num] = 0
    Frequency_map[right_num] += 1
    if(1 in Frequency_map):
      One_Count = Frequency_map[1]
    else:
      One_Count = 0
  
    while((end-start+1 - One_Count)>k):
      left_num = arr[start]
      Frequency_map[left_num] -= 1
      if(Frequency_map[left_num] == 0):
        del Frequency_map[left_num]
      start += 1

    max_length = max(max_length,end-start+1)
  # TODO: Write your code here
  return max_length


def main():
  print(length_of_longest_substring([0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], 2))
  print(length_of_longest_substring([0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1], 3))


main()